<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
      <h1>登录</h1>
      <el-card shadow="never" class="login-card">
        <!--登录表单-->
        <el-form ref="ruleFormRef" :model="loginForm" :rules="loginRules">
          <el-form-item prop="userName">
            <el-input v-model="loginForm.userName" placeholder="请输入账户名" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" show-password placeholder="请输入密码" />
          </el-form-item>
          <el-form-item>
            <el-button style="width:350px" type="primary" :loading="loading" @click="submitLogin(ruleFormRef)">登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useRouter } from 'vue-router'
import { reactive, ref } from 'vue'
import { FormInstance, FormRules } from 'element-plus'
import { useUserStore } from '@/stores'


interface RuleForm {
  userName: string
  password: string
}

const userStore = useUserStore()
const router = useRouter()
const ruleFormRef = ref<FormInstance>()
const loginForm = reactive<RuleForm>({
  userName: process.env.NODE_ENV === 'development' ? 'admin' : '',
  password: process.env.NODE_ENV === 'development' ? 'admin123' : ''
})
const loading = ref(false)

const loginRules = reactive<FormRules<RuleForm>>({
  userName: [{ required: true, message: '请输入账户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
})

const submitLogin = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  loading.value = true
  try {
    formEl.validate(async (valid) => {
      if (valid) {
        await userStore.getUserInfo({ userName: loginForm.userName, password: loginForm.password })
        loading.value = false
        router.push('/')
      }
    })
  } catch {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  align-items: stretch;
  height: 100vh;
  background-color: #ffffff;

  .logo {
    flex: 3;
    background: rgba(38, 72, 176) url(../../assets/images/login_back.png) no-repeat center / cover;
    border-top-right-radius: 60px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    padding: 0 100px;
  }

  .form {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-left: 176px;

    .el-card {
      border: none;
      padding: 0;
    }

    h1 {
      padding-left: 20px;
      font-size: 24px;
    }

    .el-input {
      width: 350px;
      height: 44px;

      .el-input__inner {
        background: #f4f5fb;
      }
    }
  }
}
</style>